﻿<!DOCTYPE html>

<html>

<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
    <script src="~/js/frame.js"></script>
</head>
<body>
    <div id="app">
        <div id="maincontainer">
            <div class="rowcontainer">
                <div class="columncontainer">

                </div>
            </div>
        </div>
    </div>
    <script>
        loadcss(getRootPath() + "theme/" + GetSystemTheme() + "/main.css", true)
    </script>
    <script>
        var pageData = {
            ElementList: [],
            groupCount: 0,
            rowElementCount: 2
        };
        $(function () {
            BindElement();
        })

        function BindElement() {
            var param = {};
            CloudPost(param, GetRootPath() + "frame/extend/element/findMainPageElementList", function (res) {
                if (res.code == 0) {
                    pageData.ElementList = res.data;
                    var rowelementcount = pageData.rowElementCount
                    var groupcount = pageData.ElementList.length % rowelementcount == 0 ? pageData
                        .ElementList
                        .length / rowelementcount : (parseInt(pageData.ElementList.length /
                            rowelementcount)) +
                        1;
                    pageData.groupCount = groupcount;
                    var MainHtml = "";
                    for (var i = 0; i < groupcount; i++) {
                        var RowHtml = " <div class='rowcontainer'>";
                        for (var j = 0; j < rowelementcount && i * groupcount + j < pageData.ElementList.length; j++) {
                            var id = "row_" + (i + 1) + "column_" + (j + 1);
                            var ColumnHtml = "<div class='columncontainer' id='" + id + "'>"
                            ColumnHtml += "</div>"
                            RowHtml += ColumnHtml;
                        }
                        RowHtml += "</div>";
                        MainHtml += RowHtml;
                    }
                    $("#maincontainer").html(MainHtml);

                    for (var i = 0; i < groupcount; i++) {
                        for (var j = 0; j < rowelementcount && i * groupcount + j < pageData.ElementList.length; j++) {
                            var index = i * rowelementcount + j
                            var element = pageData.ElementList[index];
                            var id = "row_" + (i + 1) + "column_" + (j + 1);
                            pageData.ElementList[index]["ElementID"] = id;
                            var renderdata = {
                                elementtitle: element.ElementName,
                                elementmoreurl: element.ElementMoreUrl,
                                elementid: id
                            }
                            //刷新事件存储

                            pageData.ElementList[index]["RefreshEvent"] = function () {
                                //请求数据
                                CloudPost({}, GetRootPath() + element.ElementDataUrl,
                                    function (res) {
                                        if (res.code == 0) {
                                            //合并json
                                            for (var attr in res) {
                                                renderdata[attr] = res[attr];
                                            }
                                            var htmlcontent = Mustache.render(element.TemplatContent, renderdata);
                                            $("#" + id).html(htmlcontent);
                                        }
                                    })
                            }
                            //请求数据
                            CommonRefresh(id);
                        }
                    }
                } else {
                    OpenFail(res.data)
                }
            })
        }

        function CommonRefresh(id) {
            $(pageData.ElementList).each(function () {
                if (this.ElementID == id) {
                    this.RefreshEvent();
                }
            })
        }
    </script>

</body>

</html>